package com.lzp.utilsexample.main

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.base.BaseFragment
import com.lzp.utilsexample.R
import com.lzp.utilsexample.WifiDemoActivity
import com.lzp.utilsexample.dialog.DialogDemoActivity
import com.net.NetUtils
import com.utils.toast.ToastUtils
import com.utils.permission.PermissionConstant
import com.utils.permission.PermissionListener
import com.utils.permission.PermissionUtils
import kotlinx.android.synthetic.main.utils_example_fragment.*

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/3/24
 */
class UtilsExampleFragment : BaseFragment(), View.OnClickListener {

    private var count = 0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.utils_example_fragment, null)
    }

    override fun initView() {
        super.initView()
        net()
    }

    override fun setListener() {
        super.setListener()
        dialogTv.setOnClickListener(this)
        permission.setOnClickListener(this)
        toast.setOnClickListener(this)
        backToast.setOnClickListener(this)
        iconToast.setOnClickListener(this)
        wifi.setOnClickListener(this)
    }

    @SuppressLint("ResourceType")
    override fun onClick(v: View?) {
        var intent: Intent? = null
        when (v?.id) {
            R.id.wifi->{
                intent = Intent(activity, WifiDemoActivity::class.java)
                startActivity(intent)
            }
            R.id.dialogTv -> {
                intent = Intent(activity, DialogDemoActivity::class.java)
                startActivity(intent)
            }
            R.id.permission -> {
                requestPermission()
            }
            R.id.toast -> {
                count++
                ToastUtils.toastText("$count  $count  $count   $count  $count")
            }

            R.id.iconToast -> {
                count++
                ToastUtils.toastIcon(R.mipmap.icon_toast_success, "$count  $count  $count   $count  $count")
            }
            R.id.backToast -> {
                toast.postDelayed({
                                      count++
                                      ToastUtils.toastText("$count  $count  $count   $count  $count")
                                  }, 1000)
                activity?.finish()
            }
        }
    }

    private fun requestPermission() {
        PermissionUtils.getPermission(activity, PermissionConstant.EXTERNAL_STORAGE_GROUP, object : PermissionListener {
            override fun onGranted() {
                ToastUtils.toastText("权限获取成功")
            }

            override fun onDenied(deniedPermissions: MutableList<String>?, neverTips: Boolean) {
                ToastUtils.toastText("onDenied: 权限获取失败" + if (neverTips) "且用户选择不再提示" else "")
            }
        })

    }

    private fun net() {
        NetUtils
                .get()
                .url("https://www.wanandroid.com/banner/json")
                .build()
                .enqueue(TestBean::class.java,
                         success = {
                             Log.d("test===", "成功了 $it")
                         },
                         fail = {
                             Log.d("test===", "失败了 $it")
                         })
    }
}